https://leetcode.cn/problems/as-far-from-land-as-possible/description/
class Solution {
    typedef pair<int, int> PII;

    int dx[4] = {0, 0, 1, -1};
    int dy[4] = {1, -1, 0, 0};

public:
    int maxDistance(vector<vector<int>>& grid) {
        int m = grid.size(), n = grid[0].size();
        vector<vector<int>> vis(m, vector<int>(n, -1));
        int ret = 0;

        queue<PII> q;
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1) {
                    vis[i][j] = 0;
                    q.push({i, j});
                }
            }

        while(!q.empty())
        {
            auto [a,b]=q.front();
            q.pop();
            for(int i=0;i<4;i++)
            {
                int x=dx[i]+a,y=dy[i]+b;
                if(x>=0&&x<m && y>=0&&y<n &&vis[x][y]==-1)
                {
                    int step=vis[a][b];
                    ret=max(ret,step+1);
                    vis[x][y]=step+1;
                    q.push({x,y});
                }
            }
        }
        return ret>0?ret:-1;
        
    }
};